#include <string.h>

#include <algorithm>
#include <memory>
#include <string>

class Solution {
 public:
  bool backspaceCompare(std::string s, std::string t)
  {
    std::string s1;
    std::string t1;

    auto f = [](std::string &str, std::string& newstr) -> void {
      for (int i = 0; i != str.size(); ++i)
      {
        if (str[i] != '#')
        {
          newstr.push_back(str[i]);
        } else if (!newstr.empty())
        {
          newstr.pop_back();
        }
      }
    };

    f(s, s1);
    f(t, t1);

    return s1 == t1;
  }
};
